//- Copyright (C) 2018-2022 Chatopera Inc, <https://www.chatopera.com>
//- 
//- Licensed under the Apache License, Version 2.0 (the "License");
//- you may not use this file except in compliance with the License.
//- You may obtain a copy of the License at
//- 
//-  http://www.apache.org/licenses/LICENSE-2.0
//- 
//-  Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
extends /apps/include/layout.pug
include /mixins/client.mixin.pug

block content
    .layui-side.layui-bg-black
        .layui-side-scroll
            include /apps/contacts/include/left.pug
    .layui-body
        .layui-side-scroll
            .row
                .col-lg-12
                    h1.site-h1.ukefu-tab(style='border-top:1px solid #e6e6e6;height:26px;')
                        span.ukefu-bt
                            i.layui-icon.ukewo-btn(style='font-size:20px;') 
                            .ukefu-bt-text
                                .ukefu-bt-text-title 全部联系人
                                .ukefu-bt-text-content(style='position: absolute;right: 5px;top: 0px;')
                                    form.layui-form(name='search', action='/apps/contacts/' + (subtype) + '.html', method='get', style='float: left;')
                                        if ckind
                                            input(type='hidden' name='ckind' value= ckind)
                                            input(type='hidden' name='saveStatus' value= saveStatus)
                                        .layui-form-item
                                            .layui-inline
                                                .layui-input-inline(style='width: 218px;')
                                                    input#q.layui-input(type='text', name='q', value= q, style='height: 30px;line-height: 30px;margin-top: 4px;border: 1px solid #32c24d;', placeholder='请输入联系人姓名或电话', autocomplete='off')
                                                    i.layui-icon(onclick='search.submit()', style='position: absolute;cursor: pointer;right: 3px;top: 4px;font-size: 20px;color:#32c24d;') 
                                    if user.roleAuthMap["A02_A01_A02_B05"] || user.admin
                                        .layui-btn-group(style="margin-right: 5px !important;")
                                            a(class="layui-btn layui-btn-small", href="/apps/contacts/add.html?ckind=" + (ckind ? '?ckind=' + ckind : ''),  title="新建联系人", data-toggle="ajax", data-width="950", data-height="600")
                                                i.layui-icon &#xe608;
                                                | 新建联系人
                                    if user.roleAuthMap["A02_A01_A02_B08"] || user.admin
                                        .layui-btn-group.ukefu-btn-group(style="margin-right: 5px !important;")
                                            button(class="layui-btn layui-btn-small", href="/apps/contacts/imp.pug" + (ckind ? '?ckind=' + ckind : ''),  title="导入联系人", data-toggle="ajax", data-width="950", data-height="600")
                                                i.kfont &#xe641;
                                                | 导入
                                    if user.roleAuthMap["A02_A01_A02_B09"] || user.admin
                                        .layui-btn-group.ukefu-btn-group
                                            button(class="layui-btn layui-btn-small dropdown-menu" + (!currentOrgan ? noexport : ""))
                                                i.kfont &#xe672;
                                                | 导出
                                                i.layui-icon &#xe625;
                                            if currentOrgan
                                                ul.ukefu-dropdown-menu.layui-anim.layui-anim-upbit
                                                    li
                                                        a(href="javascript:void(0)" id="batexp" title="导出联系人" target="_blank")
                                                            i.kfont &#xe672;
                                                            | 导出选中结果
                                                    li
                                                        a(href="/apps/contacts/expsearch.html?1=1&q=" + (q ? q : "") + "&ckind=" + (ckind ? ckind : "") + "&subtype=" + (subtype ? subtype : ""), title="导出联系人" target="_blank")
                                                            i.kfont &#xe672;
                                                            | 导出当前搜索结果
                                                    li
                                                        a(href="/apps/contacts/expsearch.html?ckind=" + (ckind ? ckind : ""), title="导出联系人" target="_blank")
                                                            i.kfont &#xe672;
                                                            | 导出全部数据
                    .ukefu-tab-title(style='margin-top: 10px; margin-left: 22px')
                        include /apps/contacts/top.pug
                    table.layui-table(lay-skin='line', style='table-layout: fixed')
                        thead
                            tr.design-sortable-tr
                                th(style="width: 10px;")
                                    input#all(type='checkbox', onclick='$(\'.ids\').prop(\'checked\' , $(this).prop(\'checked\'));')
                                th 联系人姓名
                                th 手机
                                th 邮件
                                th(width='30px') 性别
                                th 生日
                                th 类型
                                th 所有者
                                th(width='120px') 操作
                        tbody(style='table-layout: fixed; word-break: break-all;')
                            if contactsList.content && contactsList.content.size() > 0
                                for contacts in contactsList.content
                                    tr.tdHasContorllor.design-dropable-td
                                        td(style="width:1%;")
                                            input(type="checkbox" class="ids" name="ids" value=contacts.id)
                                        td(title=contacts.name)
                                            a(href="/apps/contacts/detail.pug?id=" + contacts.id)= contacts.name
                                        td
                                            span.ukefu-phone-number= contacts.mobileno
                                        td(title= contacts.email)
                                            p= contacts.email
                                        td
                                            if (contacts.gender && contacts.gender == '1')
                                                span= '男'
                                            if (contacts.gender && contacts.gender == '0')
                                                span= '女'
                                            if (contacts.gender && contacts.gender == '-1')
                                                span= '未知'
                                        td #{contacts.cusbirthday}
                                        td #{contacts.ckind && uKeFuDic[contacts.ckind] ? uKeFuDic[contacts.ckind].name : ""}
                                        td #{contacts.user ? contacts.user.username : ""}
                                        td
                                            if approachable.contains(contacts.id)
                                                a(href="#", onclick="openDialogWinByContactid('" + contacts.id + "')")
                                                    i.layui-icon &#xe606;
                                                    |  聊天
                                            else
                                                a.disabled(href="#", onclick="unreachableDialogWinByContactid('" + contacts.id + "')")
                                                    i.layui-icon &#xe60f;
                                                    |  聊天
                                            a(href="/apps/contacts/detail.pug?id=" + (contacts.id ? contacts.id : ""), style="margin-left:10px;")
                                                i.layui-icon &#xe60a;
                                                |  详情
                                            if (user.roleAuthMap["A02_A01_A02_B06"] || user.admin)
                                                a(href="/apps/contacts/edit.pug?id=" + (contacts.id ? contacts.id : "") + "&ckind=" + (ckind ? ckind : ""), data-toggle="ajax" data-width="950" data-height="600" data-title="编辑联系人信息")
                                                    i.layui-icon &#xe642;
                                                    |  编辑
                                            if (user.roleAuthMap["A02_A01_A02_B07"] || user.admin)
                                                a(href="/apps/contacts/delete.pug?id=" + (contacts.id ? contacts.id : "") + "&ckind=" + (ckind ? ckind : ""), style="margin-left:10px;" data-toggle="tip" data-title="请确认是否删除联系人？")
                                                    i.layui-icon(style="color:red;") &#x1006;
                                                    |  删除
                            else
                                tr
                                    td(colspan="8" style="height:400px;")
                                        .ukefu-empty(style="background: none")
                                            i.layui-icon &#xe63a;
                                            p= '还没有联系人信息'
            .row(style='padding:5px;')
                .col-lg-12#page(style='text-align:center;')

    style.
        .noexport {
            background: grey !important;
        }

    script(src='/js/CSKeFu_Rest_Request.v1.js')
    script.
        $(document).ready(function () {
            $('#batexp').click(function () {
                var ids = "";
                var num = 0;
                $('.ids').each(function () {
                    if ($(this).prop("checked")) {
                        if (ids != "") {
                            ids += "&";
                        }
                        ids += "ids=" + $(this).val();
                        num++;
                    }
                });
                if (num > 0) {
                    $('#batexp').attr('href', '/apps/contacts/expids.html?' + ids);
                } else {
                    top.layer.alert("请先选择需要导出的联系人信息");
                    $('#batexp').attr("href", "javascript:void(0)");
                }
            });
        });
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage,
                layer = layui.layer;
            laypage({
                cont: 'page',
                pages: #{contactsList ? contactsList.totalPages : 0}, //总页数
                curr: #{contactsList ? contactsList.number + 1 : 0},
                groups: 5, //连续显示分页数
                jump: function (data, first) {
                    if (!first) {
                        location.href = "/apps/contacts/#{subtype ? subtype : 'index'}.html?ckind=#{ckind}&p=" + data.curr + "&q=" + $("#q").val();
                    }
                }
            });

            layui.use('layer', function () {
                var layer = layui.layer;
                var msg = '#{msg}';

                if (msg && msg == 'new_contacts_success')
                    layer.msg('联系人添加成功', {icon: 1, time: 1500})
                else if (msg && msg == 'new_contacts_fail')
                    layer.msg('联系人添加失败，因为存在相同skypeid', {icon: 2, time: 1500})
                else if (msg && msg == 'edit_contacts_success')
                    layer.msg('联系人编辑成功', {icon: 1, time: 1500})
                else if (msg && msg == 'edit_contacts_fail')
                    layer.msg('联系人编辑失败，因为存在相同Skype ID', {icon: 2, time: 1500})
            });
        });

        function unreachableDialogWinByContactid(id) {
            layer.msg('该联系人正在被其它客服服务或没有可触达的联系方式！', {icon: 2, time: 3000})
        }

        function openDialogWinByContactid(id) {
            var multiMediaDialogWin = parent.$('#multiMediaDialogWin');
            restApiRequest({
                path: 'contacts',
                data: {
                    ops: "approach",
                    contactsid: id
                },
                silent: true
            }).then(function (data) {
                if (data.rc == 0) {
                    // 返回可用的触达渠道列表 data.data
                    if (multiMediaDialogWin.length > 0) {
                        multiMediaDialogWin.load(function () {
                            parent.layui.element().tabChange('ukefutab', 'multiMediaDialogWin');
                        }).attr('src', '/agent/proactive.html?contactid=' + id + '&channels=' + data.data);
                    } else {
                        // 使用API调用，由SocketIO传达弹屏通知，跳转到该访客页面
                        restApiRequest({
                            path: 'contacts',
                            silent: true,
                            data: {
                                ops: 'proactive',
                                channels: data.data,
                                contactid: id
                            }
                        }).then(function (result) {
                            if (result.rc == 0) {
                                // 操作成功，等待弹屏
                            } else {
                                // 操作未成功，进行提示
                                top.layer.msg('未能成功发起会话，请稍后再试！', {icon: 2, time: 3000})
                            }
                        }, handleRestApiFail);
                    }
                } else if (data.status) {
                    handleRestApiFail(data.status, data.data);
                } else {
                    layer.msg('该联系人没有触达的方式', {icon: 2, time: 1500})
                }
            });
        }

    style.
        .design-sortable-tr {
            text-align: center;
        }

        .design-sortable-tr th {
            /*设置表格文字左右和上下居中对齐*/
            vertical-align: middle;
            text-align: left;
            /*设置表格文字在一行显示*/
            text-overflow: ellipsis;
            word-break: keep-all;
            white-space: nowrap;
        }

        .design-dropable-td td p {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
        }

